package com.snow.campuslink.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snow.campuslink.exceptionhandler.PartFailBusinessException;
import com.snow.campuslink.project.constant.Constant.RabbitConstant;
import com.snow.campuslink.project.constant.Constant.SysConstant;
import com.snow.campuslink.user.converter.dto.UserLoginDto;
import com.snow.campuslink.user.converter.dto.UserRegisterDto;
import com.snow.campuslink.user.entity.User;
import com.snow.campuslink.user.mapper.UserMapper;
import com.snow.campuslink.user.service.IUserService;
import com.snow.campuslink.utils.RedisUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static cn.hutool.core.bean.BeanUtil.beanToMap;
import static cn.hutool.core.bean.BeanUtil.copyProperties;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gaosl
 * @since 2021-01-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Resource
    RedisUtil redisUtil;
    @Override
    public User login(UserLoginDto userLoginDto) {
        String loginText=userLoginDto.getLoginText();
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.select("user_id", "username",  "user_profile", "daily_login", "level", "level_experience", "gold_coin")
                .eq("password",userLoginDto.getPassword()).and(i->i.eq("username",loginText).or().eq("email",loginText).or().eq("phone_number",loginText));
        User user= userMapper.selectOne(userQueryWrapper);

        if (user == null) {
            throw  new PartFailBusinessException(SysConstant.UserLoginServiceMsg.LOGIN_FAIL);
        }
        return user;

    }

    @Override
    public void register(UserRegisterDto userRegisterDto) {
        if (!userRegisterDto.getVerificationCode().equals(redisUtil.get(userRegisterDto.getEmail()))) {
            throw new PartFailBusinessException("验证码不正确");
        }
        Map<String, Object> map = beanToMap(userRegisterDto, false, true);
        if (userMapper.selectIsRegistered(map)!=null) {
           throw new PartFailBusinessException(SysConstant.UserLoginServiceMsg.REGISTER_FAIL);
        }
        User user = new User();
        copyProperties(userRegisterDto, user, false);
        userMapper.insert(user);
    }

    @Override
    public void verificationCode(String email) {
        String verificationCode = RandomStringUtils.randomAlphanumeric(6);
        redisUtil.setEx(email,verificationCode,5, TimeUnit.MINUTES);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("email",email);
        map.put("verificationCode",verificationCode);
        rabbitTemplate.convertAndSend(RabbitConstant.SEND_VERIFICATION_CODE_EMAIL_USER_LOGIN_EXCHANGE,"", JSON.toJSON(map));
    }

    @Override
    public User getUserInfoById(Long userId) {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.select( "email", "phone_number", "real_name", "school",
                "department", "clazz", "student_number", "gender", "identity_number")
                .eq("user_id",userId);
        return userMapper.selectOne(userQueryWrapper);
    }

    @Override
    public String getUsernameById(Long userId) {
       return userMapper.getUsernameById(userId);
    }


}
